package com.example.ble_demo

import android.bluetooth.BluetoothDevice
import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.example.ble_demo.bean.DeviceInfoBean

class DeviceListViewModel : ViewModel() {
    var deviceList = MutableLiveData<MutableList<DeviceInfoBean>>()
    private var _deviceList = mutableSetOf<DeviceInfoBean>()
    var blueToothDeviceList = mutableSetOf<BluetoothDevice>()

    fun addDevice(device: DeviceInfoBean) {
        Log.d("DeviceListViewModel", "addDevice: $device")
        _deviceList.add(device)
        Log.d("DeviceListViewModel", "_deviceList ${_deviceList.size}")
        deviceList.postValue(_deviceList.toMutableList())
    }

    fun updateDevice(device: DeviceInfoBean) {
        _deviceList.removeIf { it.mac == device.mac }
        _deviceList.add(device)
        deviceList.value = _deviceList.toMutableList()
    }

    fun clearDevice() {
        _deviceList.clear()
        blueToothDeviceList.clear()
        deviceList.value = _deviceList.toMutableList()
    }
}